import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_study/activity/ScanActivity.dart';
import 'package:flutter_app_study/activity/WebActivity.dart';
import 'package:flutter_app_study/activity/ProviderActivity.dart';
import 'package:flutter_app_study/bean/CounterModel.dart';
import 'package:flutter_app_study/bean/up_data_entity.dart';
import 'package:flutter_app_study/generated/json/up_data_entity_helper.dart';
import 'package:flutter_app_study/utils/DioUtil.dart';
import 'package:flutter_app_study/view/DialogWidget.dart';
import 'package:flutter_custom_dialog/flutter_custom_dialog.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:provider/provider.dart';

import '../../cost/ErpUrl.dart';

class ChannelFragment extends StatefulWidget {
  @override
  _ChannelFragmentState createState() => _ChannelFragmentState();
}

class _ChannelFragmentState extends State<ChannelFragment> {
  var upDataEntity;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 200),
      child: Center(
        child: Column(
          children: <Widget>[
            InkWell(
              onTap: () {
                request();
              },
              child: Container(
                child: Text("点我请求"),
                padding: EdgeInsets.all(10),
              ),
            ),
            GestureDetector(
              child: Container(
                child: Text("provider测试"),
                padding: EdgeInsets.all(10),
              ),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ProviderActivity()));
              },
            ),
            GestureDetector(
              child: Container(
                child: Text("扫码测试"),
                padding: EdgeInsets.all(10),
              ),
              onTap: () {
                Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => ScanActivity("跳转传值测试")));
              },
            ),
            GestureDetector(
              child: Container(
                child: Text("webView测试"),
                padding: EdgeInsets.all(10),
              ),
              onTap: () {
                Navigator.push(context,
                    MaterialPageRoute(builder: (context) => WebActivity()));
              },
            ),
            GestureDetector(
              child: Container(
                child: Text("极光分享"),
                padding: EdgeInsets.all(10),
              ),
              onTap: () {
                shareDialog(context);
              },
            ),
            GestureDetector(
              child: Container(
                child: Text("系统分享"),
                padding: EdgeInsets.all(10),
              ),
              onTap: () {
                sysShare();
              },
            ),
            Text(upDataEntity == null ? "" : upDataEntity.updateInfo),
          ],
        ),
      ),
    );
  }

  void request() {
    EasyLoading.showProgress(0.3);
    DioUtil.post(ErpUrl.checkUpdateUrl, success: (data) {
      upDataEntity = UpDataEntity();
      upDataEntityFromJson(upDataEntity, data);
//      Logger().w(upDataEntity.toString());
      setState(() {});
      EasyLoading.showSuccess("成功");
    });
  }

  void shareDialog(BuildContext context) {
    var yyDialog = YYDialog();
    yyDialog.build(context)
      ..gravity = Gravity.bottom
      ..gravityAnimationEnable = true
      ..widget(ShareDialogWidget((index) {
        if (index == 0) {
//          share(JSharePlatform.qq);
        } else if (index == 1) {
//          share(JSharePlatform.wechatSession);
        }
        yyDialog.dismiss();
      }))
      ..show();
  }

//  void share(JSharePlatform platform) {
//    JShare jShare = new JShare();
//    JShareConfig shareConfig =
//        new JShareConfig(appKey: "5ac84d4f9bd4b84332f21df3");
//    shareConfig.channel = "channel";
//    shareConfig.isDebug = true;
//    shareConfig.isAdvertisinId = true;
//    shareConfig.isProduction = true;
//
//    shareConfig.weChatAppId = "wxabcdeb10a7de65d4";
//    shareConfig.weChatAppSecret = "dcad950cd0633a27e353477c4ec12e7a";
//
//    shareConfig.qqAppId = "1109659618";
//    shareConfig.qqAppKey = "glFYjkHQGSOCJHMC";
//
//    jShare.setup(config: shareConfig);
//    JShareMessage message = new JShareMessage();
//    message.platform = platform;
//    message.mediaType = JShareType.link;
//    message.text = "jshare-text";
//    message.title = "jshare-title";
//    message.url =
//        "https://shop.esys.eu/media/image/6f/8f/af/amlog_transport-berwachung.jpg";
//    jShare.shareMessage(message: message).then((JShareResponse response) {
//      Fluttertoast.showToast(msg: response.toJsonMap().toString());
//    }).catchError((error) {
//      print("分享回调 -- 出错：${error.toString()}");
//    });
//  }

  Future<void> sysShare() async {
//    final ByteData bytes1 = await rootBundle.load('images/1.png');
//    final ByteData bytes2 = await rootBundle.load('images/2.png');
//    final ByteData bytes3 = await rootBundle.load('images/3.png');
//    await Share.files(
//        'esys images',
//        {
//          'esys.png': bytes1.buffer.asUint8List(),
//          'bluedan.png': bytes2.buffer.asUint8List(),
//          'addresses.csv': bytes3.buffer.asUint8List(),
//        },
//        '*/*',
//        text: 'My optional text.');
//    EasyLoading.show(status: "处理中");
//    var request = await HttpClient().getUrl(Uri.parse(
//        'https://shop.esys.eu/media/image/6f/8f/af/amlog_transport-berwachung.jpg'));
//    var response = await request.close();
//    Uint8List bytes = await consolidateHttpClientResponseBytes(response);
//    await Share.file('测试系统分享功能', 'amlog.jpg', bytes, 'image/jpg',
//        text: "测试系统分享功能");
//    EasyLoading.dismiss();
  }
}
